package com.wetao.module_report.adapter;

import android.content.Context;
import android.graphics.Color;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import com.wetao.module_report.R;
import com.wetao.module_report.R2;
import com.wetao.lib_common.bean.response.GetSubjectList;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import q.rorbin.badgeview.Badge;
import q.rorbin.badgeview.QBadgeView;

public class ReportMain_h_Adapter extends RecyclerView.Adapter{
    private List<GetSubjectList> mData= new ArrayList<>();
    private int mCurPosition;
    private Context mContext;
    public ReportMain_h_Adapter(Context context) {
        mContext = context;
    }

    public void updateData(List<GetSubjectList> data) {
        mData = data;
        notifyDataSetChanged();
    }

    public void setmCurPosition(int position) {
        mCurPosition = position;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if(viewType == 1) {
            View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.report_main_h_item, parent, false);
            return new HHolder(view);
        }
        return null;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
        if (holder instanceof HHolder) {
            ((HHolder)holder).bindData(position);
        }
    }

    @Override
    public int getItemCount() {
        if(mData == null) {
            return 0;
        }
        return mData.size();
    }

    @Override
    public int getItemViewType(int position) {
        return 1;
    }


    class HHolder extends RecyclerView.ViewHolder{

        @BindView(R2.id.tv_subject)
        TextView tv_subject;
        @BindView(R2.id.v_select)
        View v_select;

        public HHolder( View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }

        public void bindData(int position) {
            clickListener(position);
            tv_subject.setText(mData.get(position).getSubjectName());
            if(position == mCurPosition) {
                tv_subject.setTextColor(Color.parseColor("#00A8B2"));
                v_select.setVisibility(View.VISIBLE);
            } else {
                tv_subject.setTextColor(Color.parseColor("#666666"));
                v_select.setVisibility(View.GONE);
            }
        }
        private void clickListener(int position) {
            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (listener != null && mCurPosition != position) {
                        mCurPosition = position;
                        notifyDataSetChanged();
                        listener.onItemClick(position);
                    }
                }
            });
        }
    }

    //第一步 定义接口
    public interface OnItemClickListener {
        void onItemClick(int position);
    }
    private OnItemClickListener listener;
    //第二步， 写一个公共的方法
    public void setOnItemClickListener(OnItemClickListener listener) {
        this.listener = listener;
    }

}
